#pragma once
#include <stdint.h>
#include <cstddef> //size_t 的标准位置
#ifdef WINDOWS
#define EXPORT_API extern "C" __declspec(dllexport)
#else
#define EXPORT_API extern "C"
#endif
//App_MainLoop
typedef void (*deleOnInit)();
typedef void (*deleOnUpdate)();
typedef void (*deleOnGUI)();
typedef void (*deleOnExit)();
typedef void (*deleOnLog)(const char16_t* log);
EXPORT_API int APP_SetLogger(deleOnLog logger);
EXPORT_API int App_MainLoop(const char16_t *title, deleOnInit onInit, deleOnUpdate onUpdate, deleOnGUI onGUI, deleOnExit onExit);

EXPORT_API void App_UpdateFontTex();
//ImGUI �� Assert����
typedef void (*deleOnIMAssertU16)(const char16_t *_expr, const char16_t *filename, int line);
EXPORT_API void SetAssertCallBack(deleOnIMAssertU16 callback);

//string tool

//�������������ȫָ��������������ˣ��ڲ�תȥ������ӿ�ͳһutf16

//EXPORT_API char16_t* ToUtf16String(char* strAnsiOrUtf8);
//EXPORT_API char* ToUtf8String(char16_t* strUtf16);

///////////////////////////////////////////////////////////////////////////
//����
//��Ȼ���˰��쾢������Ч��̫�ͣ�ȫ��ɾ����ͷ����

enum class VertexFormat
{
	Nothing,
	Pos,
	Pos_Color0,
	Pos_UV0_Color0,
	Pos_UV0_UV1_Color0_Color1,
	Pos_UV0_UV1_UV2_Color0_Color1_Color2,
	Pos_UV0_UV1_UV2_UV3_Color0_Color1_Color2_Color3,
};

//��Щ���ⶼֻ�Ǹ�Handle���ѣ�����Ҫ����ʵ��
struct Target;
struct Texture;
struct Material;
struct Mesh;

//struct TargetHandle
//{
//	Target* ptr;
//};
//�����������һ����ۣ�player �� MainScreen ��������MainScreen ���Ƕ�Editor��˵MainScreen ��һ��RenderTarget
EXPORT_API Target *Target_Create(int width, int height);
EXPORT_API void Target_Destory(Target *handle);
EXPORT_API void Target_Reset(Target *handle, int width, int height);

//���������������ֻ��Editor��Ҫ
EXPORT_API size_t Target_GetImGuiTexID(Target *handle);

EXPORT_API void MainScreen_Reset(int width, int height);

//�@�Yֻ��һ��BeginFrame�漰Target
EXPORT_API void Frame_SetTarget(Target *handle);
EXPORT_API void Frame_SetMainScreen();

//ÿһ��Clear����һ��Begin��Ҫ��Ҫ���Ǳ�¶bgfx��viewid����Ϊ�Ȿ������һ�ֹ��������
EXPORT_API void Frame_Clear(uint32_t color, bool withColor, bool withDepth);
EXPORT_API void Frame_Draw(Material *mat, Mesh *mesh);
EXPORT_API void Frame_Flush();
//struct TextureHandle
//{
//	Texture* ptr;
//};
//struct Material;
//struct MaterialHandle
//{
//	Material* tex;
//};
////========================================
////BGFX�Ľӿ�̫�����ˣ����ǻ��Ǹ��������Դ��Mesh �� Material
////Unity��Shader����ܴ��漰Ҳ�ܹ�

////========================================
//texture������ѹ����
struct XYWH
{
	int x;
	int y;
	int w;
	int h;
};
struct Vector4
{
	float x;
	float y;
	float z;
	float w;
};
EXPORT_API Texture *Texture_LoadFromFile(const char16_t *filename);
EXPORT_API Texture *Texture_LoadFromMem(const char *mem, uint32_t size);
EXPORT_API Texture *Texture_Create(uint32_t width, uint32_t height, const char *memInit = nullptr);
EXPORT_API size_t Texture_GetImGuiTexID(Texture *handle);

EXPORT_API void Texture_Upload(Texture *texture, XYWH xywh, const char *mem, int size, int pitch);
EXPORT_API void Texture_Destory(Texture *handle);

EXPORT_API Material *Material_Create(const char16_t *shadername);
EXPORT_API void Material_Destory(Material *material);
EXPORT_API int Material_GetUniformCount(Material *material);
EXPORT_API const char16_t *Material_GetUniformName(Material *material, int index);
EXPORT_API size_t Material_GetUniformNameHash(Material *material, int index);
enum class UniformType : uint8_t //��bgfx����һ��
{
	Sampler, //!< Sampler. ��ͼ
	End,	 //!< Reserved, do not use.

	Vec4, //!< 4 floats vector.
	Mat3, //!< 3x3 matrix.
	Mat4, //!< 4x4 matrix.

	Count
};
EXPORT_API UniformType Material_GetUniformType(Material *material, int index);
EXPORT_API void Material_SetTexture(Material *material, int uniformID, Texture *texture);
EXPORT_API void Material_SetVec4(Material *material, int uniformID, Vector4 value);

//��ͨģ�ͱ��봴��ͬʱ��ʼ��
EXPORT_API Mesh *Mesh_CreateSample();
EXPORT_API Mesh *Mesh_Create(VertexFormat format, int vertexCount, int triCount, const char *memVB = nullptr, const uint16_t *memIB = nullptr);
EXPORT_API void Mesh_Destory(Mesh *handle);
EXPORT_API int Mesh_GetVertexStride(Mesh *handle);

//��̬ģ�Ϳ�����ʱ����
EXPORT_API Mesh *MeshDynamic_Create(VertexFormat format, int vertexCount, int triCount);
EXPORT_API void MeshDynamic_UploadVertex(Mesh *mesh, int VertexStart, int VertexCount, const char *mem);
EXPORT_API void MeshDynamic_UploadIndex(Mesh *mesh, int IndexStart, int IndexCount, const uint16_t *indexdata);
